package com.hwhp.web.example.action;

import javax.servlet.http.HttpServletRequest;

import com.hwhp.common.BaseAction;
import com.hwhp.common.Pager;
import com.hwhp.entity.DcpUser;
import com.hwhp.service.UserService;


@SuppressWarnings("serial")
public class UserAction extends BaseAction{
	
	private  UserService userService;
	public String login(){
		  return "input";
	
	}

	public String register(){
	   
	   return "input";
	}
	
	public String listuser(){
	   HttpServletRequest request = this.getHttpRequest();
	   queryUserByPage(request);
	   return "listSuccess";
	}
	
	private void queryUserByPage(HttpServletRequest request) {
		Pager page = new Pager();
		   int pageNumber = 1;
		   try{
			   try{
				   pageNumber = Integer.parseInt(request.getParameter("pageNumber"));
			   }catch(Exception e){
				   pageNumber = 1;
				   e.printStackTrace();
			   }
			   String hqlStr = "from DcpUser as u";
			   page = userService.queryUser(hqlStr,pageNumber,3);
		   }catch(Exception e){
			   e.printStackTrace();
		   }
		   
		   request.setAttribute("page", page);
		   request.setAttribute("pageNumber",pageNumber);
		   
	}
    public String userUpdateQuery(){
     
      return "userUpdateQuerySuccess";
    }	
    public String userupdate(){

      return "listSuccess";
    	
    }
    public String userdelete(){
     
      return "listSuccess";
    }
  
    
    public String addClass(){
    	boolean success = false ;
    	HttpServletRequest request = this.getHttpRequest();
    	String userName = request.getParameter("username");
    	String password = request.getParameter("password");
    	DcpUser user = new DcpUser();
    	user.setUsername(userName);
    	user.setPassword(password);
    	try{
    		success  = userService.insertUser(user);
    	}catch(Exception e){
    		success = false ;
    		e.printStackTrace();
    	}
    	queryUserByPage(request);
    	return "listSuccess";
    	
    }

    
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public UserService getUserService() {
		return userService;
	}
    
    
}
